package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.MessageComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.CustomerMarkInfo;
import com.dy.lock.screen.pojo.MessageInfo;
import com.dy.lock.screen.pojo.UserInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CommonCommentService;
import com.dy.lock.screen.service.CustomerLikeService;
import com.dy.lock.screen.service.CustomerMarkRelationService;
import com.dy.lock.screen.service.MessageService;
import com.dy.lock.screen.service.UserService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.CommonCommentLikeVo;
import com.dy.lock.screen.vo.MessageVo;
import com.google.gson.Gson;

@Component("messageComponent")
public class MessageComponentImpl implements MessageComponent {
	private static final Logger log = Logger
			.getLogger(MessageComponentImpl.class);

	private Gson gson;

	@Resource(name = "messageService")
	private MessageService messageService;
	@Resource(name = "commonCommentService")
	private CommonCommentService commonCommentService;
	@Resource(name = "customerLikeService")
	private CustomerLikeService customerLikeService;
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "customerMarkRelationService")
	private CustomerMarkRelationService customerMarkRelationService;

	public MessageComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<MessageVo> getCustomerMessage(String body, Head head) {
		MessageVo vo = gson.fromJson(body, MessageVo.class);
		if (vo != null && StringUtils.isNotBlank(vo.getUserId())) {
			List<MessageInfo> re = this.messageService.getCustomerMessage(vo
					.getUserId());
			if (re != null) {
				List<MessageVo> returnList = new ArrayList<MessageVo>();
				for (MessageInfo messageInfo : re) {
					MessageVo messageVo = new MessageVo();
					messageVo.setMessageId(messageInfo.getMessageId());
					messageVo.setContent(messageInfo.getMessageContent());
					messageVo.setDate(messageInfo.getSendTime());
					messageVo.setTitle(messageInfo.getMessageTitle());
					messageVo.setType(messageInfo.getMessageType());
					returnList.add(messageVo);
				}
				return returnList;
			} else {
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取用户消息，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CommonCommentLikeVo getMessageCommonCommentOrLike(String body,
			Head head) {
		CommonCommentLikeVo vo = gson.fromJson(body, CommonCommentLikeVo.class);
		if (vo != null) {
			CommonCommentLikeVo info = null;
			if (vo.isCommonOrLike()) {
				String commonCommentId = vo.getCommonCommentId();
				info = this.commonCommentService
						.queryDetailByPrimaryKey(commonCommentId);
			} else {
				String id = vo.getId();
				info = this.customerLikeService.queryDetailByPrimaryKey(id);
			}
			if (info != null) {
				String userId = info.getUserId();
				UserInfo userInfo = this.userService
						.queryByUserId(userId, head);
				String username = userInfo.getUserName();
				if (StringUtils.isNotBlank(username)) {
					info.setUserName(username);
				} else {
					info.setUserName(userId);
				}
				if (StringUtils.isNoneBlank(userInfo.getHeaderPath())) {
					info.setHeaderPath(CommonUtil
							.getUrlPrefixApi("photo.url.prefix.path.api")
							+ userInfo.getHeaderPath());
				}
				info.setCommentPicPath(CommonUtil.splitToString(info
						.getCommentPicPath(), ";"));
				if (StringUtils.isNotBlank(info.getMaterialPic())) {
					info.setMaterialPic(CommonUtil
							.getUrlPrefixApi("photo.url.prefix.path")
							+ info.getMaterialPic());
				}
				if (StringUtils.isNotBlank(vo.getCommonCommentId())) {
					String atIds = info.getHaveAt();
					List<UserInfo> userList = CommonUtil.getUserListByUserIds(
							userService, atIds);
					info.setAtUserList(userList);
				}
				List<CustomerMarkInfo> markInfos = this.customerMarkRelationService
						.queryListByCid(userId);
				info.setMarkInfoList(markInfos);
			}

			return info;
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取消息评论或赞详情时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
